class Solution {
public:
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int maxArea{0};
        int curArea{0};
        for (int sr = 0; sr < grid.size(); sr++) {
            for (int sc = 0; sc < grid[sr].size(); sc++) {
                if (grid[sr][sc] == 1) {
                    GetArea(grid, sr, sc, curArea);
                    maxArea = max(maxArea, curArea);
                    curArea = 0;
                }
            }
        }
        return maxArea;
    }
    
    void GetArea(vector<vector<int>>& grid, int sr, int sc, int& area)
    {
        if((sr < 0) || (sc < 0) || (sr >= grid.size()) || (sc >= grid[sr].size())) {
            return;
        }
        if (grid[sr][sc] == 0) {
            return;
        }
        area++;
        grid[sr][sc] = 0;
        GetArea(grid, sr - 1, sc, area);
        GetArea(grid, sr + 1, sc, area);
        GetArea(grid, sr, sc - 1, area);
        GetArea(grid, sr, sc + 1, area);
    }
};